package com.ujiuye.controller;

import com.ujiuye.model.Page;
import com.ujiuye.model.Student;
import com.ujiuye.service.impl.StudentServiceImpl;
import com.ujiuye.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

//@MultipartConfig   //处理文件上传，如果不添加，获取的数据为空
//@WebServlet("*.do")
public class StudentServlet extends BaseServlet {
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        String sbir = req.getParameter("sbir");
        String[] hobby = req.getParameterValues("hobby");
        //获取上传文件
        Part part = req.getPart("file");
        //获取上传的文件名称
        String photo = part.getSubmittedFileName();
        //为了解决文件重复的问题，使用UUID重复生成文件名称
        // 获取上传文件的类型
        String suffix = photo.substring(photo.lastIndexOf("."));
        // 为了解决文件名重复的问题,使用UUID重新生成文件名称
        String  filename = UUID.randomUUID().toString() + suffix;
        String path = "D:\\资料\\img";
        //File.separator 磁盘分隔符
        part.write(path+ File.separator+filename);
        Student student = new Student(sname,gender, DateUtils.strTransformerDate(sbir, "yyyy-MM-dd")
                ,Arrays.toString(hobby),filename);
        StudentServiceImpl ss = new StudentServiceImpl();
        boolean b = ss.saveStudent(student);
        if(b){
            resp.sendRedirect("http://localhost:8080/StudentManager_war_exploded/queryStu.do");
        }else{
            req.getRequestDispatcher("addStu.jsp").forward(req,resp);
        }
    }

    public void queryStu(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //1.获取参数
        String currentPage = req.getParameter("currentPage");
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        // 2.数据封装
        Student student = new Student(sname, gender);
        // 3. 调用业务逻辑层方法
        // 调用业务逻辑层方法
        StudentServiceImpl ss = new StudentServiceImpl();
        Page<Student> page = ss.stuPageList(currentPage, student);

        // 4.页面跳转
        req.setAttribute("page",page);
        // 条件的回显
        req.setAttribute("student",student);
        req.getRequestDispatcher("page/queryStu.jsp").forward(req, resp);
    }


    public void deleteStudentById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String ids = req.getParameter("ids");
        StudentServiceImpl ss = new StudentServiceImpl();
        int num = ss.deleteStudentById(ids);
        System.out.println("num = " + num);
        resp.sendRedirect("http://localhost:8080/StudentManager_war_exploded/queryStu.do");
    }
//根据学生编号获取学生信息并回显
    public void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentServiceImpl ss = new StudentServiceImpl();
        Student student = ss.updateStudentByIds(sid);
        req.setAttribute("student",student);
        req.getRequestDispatcher("page/updateStu.jsp").forward(req,resp);
    }


    public void updateStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        String sname = req.getParameter("sname");
        String gender = req.getParameter("gender");
        String sbir = req.getParameter("sbir");
        String[] hobby = req.getParameterValues("hobby");
        String oldPhoto = req.getParameter("photo");
        Part file = req.getPart("file");
        //获取上传文件的名称
        String photos = file.getSubmittedFileName();
        if(!"".equals(photos)){
            // 获取上传文件的类型
            String suffix = photos.substring(photos.lastIndexOf("."));
            // 如果有文件上传，就覆盖源文件的名称
            oldPhoto = UUID.randomUUID().toString() + suffix;
            String path = "D:\\资料\\img";
            //File.separator 磁盘分隔符
            file.write(path + File.separator + oldPhoto);
        }
            Student student = new Student(sname, gender, DateUtils.strTransformerDate(sbir, "yyyy-MM-dd"),
                    Arrays.toString(hobby), oldPhoto);
            student.setSid(Integer.parseInt(sid));
        StudentServiceImpl ss = new StudentServiceImpl();
        boolean b = ss.updateStudentDo(student);
        resp.sendRedirect("queryStu.do");
    }
}
